package service;

import domain.*;
import persistence.*;
import persistence.impl.*;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OrderService {

    private ItemDao itemDao;
    private OrderDao orderDao;
    private SequenceDao sequenceDao;
    private LineItemDao lineItemDao;
    private CartShopDao cartShopDao;
    public OrderService(){
        itemDao = new ItemDaoImpl();
        orderDao = new OrderDaoImpl();
        sequenceDao = new SequenceDaoImpl();
        lineItemDao = new LineItemDaoImpl();
        cartShopDao = new CartShopDaoImpl();
    }

    public void insertOrder(Order order)
    {
        order.setOrderId(getNextId("ordernum"));
        for(int i=0;i<order.getLineItems().size();i++)
        {
            LineItem lineItem=order.getLineItems().get(i);
            String itemId=lineItem.getItemId();
            Integer increment=lineItem.getQuantity();
            Map<String,Object> param=new HashMap<>(2);
            param.put("itemId",itemId);
            param.put("increment",increment);
            itemDao.updateInventoryQuantity(param);
        }
        orderDao.insertOrder(order);
        orderDao.insertOrderStatus(order);
        for(int i=0;i<order.getLineItems().size();i++)
        {
            LineItem lineItem=order.getLineItems().get(i);
            lineItem.setOrderId(order.getOrderId());
            lineItemDao.insertLineItem(lineItem);
        }
    }

    public Order getOrder(int orderId)
    {
        Order order= orderDao.getOrder(orderId);
        List<LineItem> lineItems = lineItemDao.getLineItemsByOrderId(orderId);
        order.setLineItems(lineItemDao.getLineItemsByOrderId(orderId));
        for(int i=0;i<order.getLineItems().size();i++)
        {
            LineItem lineItem=order.getLineItems().get(i);
            Item item=itemDao.getItem(lineItem.getItemId());
            item.setQuantity(itemDao.getInventoryQuantity(lineItem.getItemId()));
            lineItem.setItem(item);
        }
        return order;
    }

    public List<Order> getOrderByUsername(String username)
    {
        return orderDao.getOrdersByUsername(username);
    }

    public int getNextId(String name) {
        Sequence sequence = new Sequence(name, -1);
        sequence = sequenceDao.getSequence(sequence);
        if (sequence == null) {
            throw new RuntimeException("Error: A null sequence was returned from the database (could not get next " + name
                    + " sequence).");
        }
        Sequence parameterObject = new Sequence(name, sequence.getNextId() + 1);
        sequenceDao.updateSequence(parameterObject);
        return sequence.getNextId();
    }
    public List<CartItem> getCartShopItems(Account account) throws SQLException {
        return cartShopDao.getCartShopItems(account);
    }
    public void insertIntoCartShop(Account account,CartItem cartItem)
    {
        cartShopDao.insertIntoCartShop(account,cartItem);
    }
    public void deleteItemFromCartShop(Account account,CartItem cartItem)
    {
        cartShopDao.deleteItemFromCartShop(account,cartItem);
    }
    public void updateItemQuantity(Account account,CartItem cartItem)
    {
        cartShopDao.updateItemQuantity(account,cartItem);
    }
    public void deleteAll(Account account)
    {
        cartShopDao.deleteAll(account);
    }

    public static void main(String[] args) {
        OrderService orderService = new OrderService();
        int orderId = 1035;
        Order order = orderService.getOrder(orderId);
        System.out.println(order.getLineItems());
    }

}
